package org.gurms.web.action.system;

import java.util.List;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.gurms.common.config.GlobalConfig;
import org.gurms.common.config.GlobalMessage;
import org.gurms.common.util.EncryptUtil;
import org.gurms.service.dto.system.MenuDto;
import org.gurms.service.dto.system.UserDto;
import org.gurms.service.system.SysMenuService;
import org.gurms.service.system.SysUserService;
import org.gurms.web.action.BaseAction;
import org.gurms.web.action.Struts2ActionUtil;
import org.springframework.beans.factory.annotation.Autowired;

@Results({
	@Result(name=BaseAction.RESULT_DIVCSS, location="../login",type="redirect"),
	@Result(name=BaseAction.RESULT_FRAMESET, location="../frameset-login",type="redirect"),
	@Result(name=BaseAction.RESULT_INDEX, location="/index.jsp",type="redirect")
})
public class SysLoginAction extends BaseAction {

	private UserDto user = new UserDto();
	
	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private SysMenuService sysMenuService;
	
	public String execute() throws Exception{
		UserDto u = sysUserService.get(user);
		String pw = EncryptUtil.md5Encode(user.getLoginpassword());
		if (u != null && u.getLoginpassword().equals(pw)) {
			List<MenuDto> menus = null;
			if(u.getRoles().contains(GlobalConfig.ID_SYS_ROLE_SYSTEM)){
				menus = sysMenuService.getList(GlobalConfig.ID_SYS_MENU_ROOT);
			}
			
			Struts2ActionUtil.getSession().setAttribute(S_KEY_USER, u);
			Struts2ActionUtil.getSession().setAttribute(S_KEY_MENU, menus);
			return RESULT_CONFIG;
		}else{
			addActionMessage(GlobalMessage.getMessage("9101"));
			return RESULT_INDEX;
		}
	}

	//----------------------getter/setter---------------------
	public UserDto getModel() {
		return user;
	}
	
	public void setSysUserService(SysUserService userservice) {
		this.sysUserService = userservice;
	}

	public void setSysMenuService(SysMenuService sysMenuService) {
		this.sysMenuService = sysMenuService;
	}
		
}
